﻿using System;
using System.Collections;
using System.Runtime.Caching;
using System.Web;
using Model.Sys;

namespace Common
{
    public class WebCacheHelper
    {
        private static System.Web.Caching.Cache webCache = HttpRuntime.Cache;//全局公用WebCache

        #region 增加缓存

        /// <summary>
        /// 写入缓存,永不过期
        /// </summary>
        public static bool AddCache(string key, object value)
        {
            Cache cache = new Cache()
            {
                CreateTime = DateTime.Now,
                FailureTime = ObjectCache.InfiniteAbsoluteExpiration.DateTime,
                Value = value
            };

            return webCache.Add(key, cache, null, cache.FailureTime, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.NotRemovable, null) != null;
        }

        /// <summary>
        /// 写入缓存，自定义过期时间
        /// </summary>
        public static bool AddCache(string key, object value, DateTime expireTime)
        {
            Cache cache=new Cache()
            {
                CreateTime = DateTime.Now,
                FailureTime = expireTime,
                Value = value
            };

            return webCache.Add(key, cache, null, cache.FailureTime, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.NotRemovable, null) != null;
        }

        #endregion

        #region 删除缓存

        /// <summary>
        /// 移除指定数据缓存
        /// </summary>
        public static bool RemoveCache(string key)
        {
            return webCache.Remove(key)!=null;
        }

        /// <summary>
        /// 移除全部缓存
        /// </summary>
        public static bool RemoveCache()
        {
            IDictionaryEnumerator cacheEnum = webCache.GetEnumerator();
            while (cacheEnum.MoveNext())
            {
                webCache.Remove(cacheEnum.Key.ToString());
            }
            return true;
        }

        #endregion

        #region 修改缓存

        /// <summary>
        /// 修改缓存对象
        /// </summary>
        public static bool UpdateCache(string key, object value)
        {
            Cache cache=(Cache)webCache.Get(key);

            cache.Value = value;
            return true;
        }

        #endregion

        #region 查询缓存

        /// <summary>
        /// 缓存个数
        /// </summary>
        /// <returns></returns>
        public static int GetCacheCount()
        {
            return webCache.Count;
        }

        /// <summary>
        /// 是否存在对应key值的缓存
        /// </summary>
        public static bool IsExistCache(string key)
        {
            return webCache.Get(key) != null;
        }

        /// <summary>
        /// 读取缓存
        /// </summary>
        public static Cache GetCache(string key)
        {
            if (webCache[key] != null)
            {
                return (Cache)webCache[key];
            }
            return null;
        }

        #endregion

    }
}
